package com.maolong.deadconsume.service;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
@Slf4j
public class NormalConsume {

    @RabbitListener(queues = "normal_queue")
    public void normalConsume(String body, Channel channel, Message message) throws IOException {
        try {
            if("3".equals(body)){
                log.info("收到异常队列信息{}",body);
                int ret = 1 / 0;
            }else {
                log.info("收到队列信息{}", body);
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            if(message.getMessageProperties().getRedelivered()){
                log.info("拒绝");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            }else{
                log.info("重新投递");
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
            }
        }
    }
}
